// 设置打印函数
const print = globalThis.print || console.log;

// 设置断言函数
const assert = (condition, message, actual) => {
    const status = condition ? "✓" : "✗";
    print(`${status} 测试用例: ${message}`);
    if (!condition) {
        print(`  期望值: ${condition}`);
        print(`  实际值: ${actual}`);
    }
    return condition;
};

let totalTests = 0;
let passedTests = 0;

function runTest(name, fn) {
    totalTests++;
    print(`\n执行测试: ${name}`);
    try {
        if (fn()) {
            passedTests++;
        }
    } catch (error) {
        print(`  执行出错: ${error.message}`);
    }
}

// 1. 基础对象属性访问
const basicObj = {
    name: "Alice",
    age: 25,
};
runTest("基础对象属性访问", () => {
    const nameResult = Reflect.get(basicObj, "name");
    const notExistResult = Reflect.get(basicObj, "notExist");
    return (
        assert(nameResult === "Alice", "基础属性访问", nameResult) &&
        assert(notExistResult === undefined, "访问不存在的属性", notExistResult)
    );
});

// 2. getter 方法测试
const objWithGetter = {
    _name: "Bob",
    get name() {
        return this._name;
    },
};
runTest("getter 方法测试", () => {
    const defaultResult = Reflect.get(objWithGetter, "name");
    const customResult = Reflect.get(objWithGetter, "name", {
        _name: "Charlie",
    });
    return (
        assert(defaultResult === "Bob", "默认 getter", defaultResult) &&
        assert(customResult === "Charlie", "使用自定义 receiver", customResult)
    );
});

// 3. 数组操作
const array = ["apple", "banana", "orange"];
runTest("数组操作", () => {
    const indexResult = Reflect.get(array, 1);
    const lengthResult = Reflect.get(array, "length");
    return (
        assert(indexResult === "banana", "数组索引访问", indexResult) &&
        assert(lengthResult === 3, "数组长度", lengthResult)
    );
});

// 4. 原型链测试
const parent = { parentProp: "parent value" };
const child = Object.create(parent);
child.childProp = "child value";
runTest("原型链测试", () => {
    const childResult = Reflect.get(child, "childProp");
    const parentResult = Reflect.get(child, "parentProp");
    return (
        assert(childResult === "child value", "子对象属性访问", childResult) &&
        assert(parentResult === "parent value", "继承的属性", parentResult)
    );
});

// 5. Symbol 属性
const nameSymbol = Symbol("name");
const ageSymbol = Symbol("age");
const objWithSymbols = {
    [nameSymbol]: "David",
    [ageSymbol]: 30,
};
runTest("Symbol 属性", () => {
    const symbolResult = Reflect.get(objWithSymbols, nameSymbol);
    return assert(symbolResult === "David", "Symbol 属性访问", symbolResult);
});

// 6. 嵌套对象
const nested = {
    user: {
        profile: {
            name: "Eve",
            settings: {
                theme: "dark",
            },
        },
    },
};
runTest("嵌套对象", () => {
    const nameResult = Reflect.get(nested.user.profile, "name");
    const themeResult = Reflect.get(nested.user.profile.settings, "theme");
    return (
        assert(nameResult === "Eve", "嵌套属性访问", nameResult) &&
        assert(themeResult === "dark", "深层嵌套访问", themeResult)
    );
});

// 7. 类实例
class Person {
    constructor(name) {
        this._name = name;
    }
    get name() {
        return this._name;
    }
}
const person = new Person("Frank");
runTest("类实例", () => {
    const nameResult = Reflect.get(person, "name");
    return assert(nameResult === "Frank", "类实例属性访问", nameResult);
});

// 8. Receiver 测试
runTest("Receiver 测试 - 基础对象", () => {
    const target = {
        _name: "original",
        get name() {
            return this._name;
        },
    };

    // 不传 receiver，this 指向 target
    const result1 = Reflect.get(target, "name");

    // 传入 receiver，this 指向 receiver
    const receiver = { _name: "changed" };
    const result2 = Reflect.get(target, "name", receiver);

    return (
        assert(
            result1 === "original",
            "不传 receiver 时使用 target 作为 this",
            result1
        ) &&
        assert(
            result2 === "changed",
            "传入 receiver 时使用 receiver 作为 this",
            result2
        )
    );
});

runTest("Receiver 测试 - 原型链", () => {
    const proto = {
        get value() {
            return this._value;
        },
    };

    const obj = Object.create(proto);
    obj._value = "obj value";

    const receiver = { _value: "receiver value" };

    // 从原型链获取 getter，但 this 绑定到 receiver
    const result = Reflect.get(obj, "value", receiver);

    return assert(
        result === "receiver value",
        "原型链上的 getter 使用 receiver 作为 this",
        result
    );
});

runTest("Receiver 测试 - 属性描述符", () => {
    const target = {};
    Object.defineProperty(target, "prop", {
        get() {
            return this.value;
        },
    });

    const receiver1 = { value: "receiver1" };
    const receiver2 = { value: "receiver2" };

    const result1 = Reflect.get(target, "prop", receiver1);
    const result2 = Reflect.get(target, "prop", receiver2);

    return (
        assert(
            result1 === "receiver1",
            "属性描述符中的 getter 使用 receiver1",
            result1
        ) &&
        assert(
            result2 === "receiver2",
            "属性描述符中的 getter 使用 receiver2",
            result2
        )
    );
});

// 打印测试总结
print(`\n测试总结:`);
print(`总计: ${totalTests} 个测试用例`);
print(`通过: ${passedTests} 个`);
print(`失败: ${totalTests - passedTests} 个`);
